﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;

namespace StudentManage
{
   public  class SqlHelper
    {
        public  static string connStr = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString ;

        public static object ExecuteScalar(string sql, int cmdType, params MySqlParameter[] param)
        {
            object obj = null;
            using (MySqlConnection conn = new MySqlConnection(connStr))
            {
                MySqlCommand cmd = new MySqlCommand(sql, conn);
                if (cmdType == 2) //执行的是一个存储过程
                    cmd.CommandType = CommandType.StoredProcedure;
                if (param != null && param.Length > 0)
                    cmd.Parameters.AddRange(param);
                //打开连接
                conn.Open();
                //执行操作
                obj = cmd.ExecuteScalar();
                //执行完毕后清除参数数组
                cmd.Parameters.Clear();
                //关闭连接 (在using中操作的话，此步可有可无)
                conn.Close();
            }
            return obj;
        }

        public static DataTable GetDataTable(string sql, params MySqlParameter[] param)
        {
            DataTable dt = new DataTable();
            using (MySqlConnection conn = new MySqlConnection(connStr))
            {
                MySqlCommand cmd = new MySqlCommand(sql, conn);
                if (param != null && param.Length > 0)
                {
                    //先清除参数数组
                    cmd.Parameters.Clear();
                    cmd.Parameters.AddRange(param);
                }
                //断开式链接
                MySqlDataAdapter da = new MySqlDataAdapter();
                da.SelectCommand = cmd;
                da.Fill(dt);
            }
            return dt;
        }

        /// <summary>
        /// 增删改
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="paras"></param>
        /// <returns></returns>
        public static int ExecuteNonQuery(string sql, params MySqlParameter[] paras)
        {
            int rs = 0;
            using (MySqlConnection conn = new MySqlConnection(connStr))
            {
                MySqlCommand cmd = new MySqlCommand(sql, conn);
                if (paras != null && paras.Length > 0)
                {
                    //先清除参数数组
                    cmd.Parameters.Clear();
                    cmd.Parameters.AddRange(paras);
                }
                conn.Open();
                //执行操作
                rs = cmd.ExecuteNonQuery();
                //执行完毕后清除参数数组
                cmd.Parameters.Clear();
                //关闭连接 (在using中操作的话，此步可有可无)
                conn.Close();
            }
            return rs;
        }


        public static MySqlDataReader ExcuteReader(string sql,params MySqlParameter[] paras)
        {
            MySqlConnection conn = new MySqlConnection(connStr);
            MySqlCommand cmd = new MySqlCommand(sql,conn);
            try
            {
                conn.Open();
                cmd.Parameters.Clear();
                cmd.Parameters.AddRange(paras);
                MySqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                return dr;
            }
            catch (MySqlException ex)
            {
                conn.Close();
                throw new Exception("查询出现异常",ex);
            }
        }
    }
}
